package com.vanvalt.payhui.modules.pay.api.business;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.vanvalt.payhui.modules.pay.api.command.impl.DownloadCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.PageCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.RequestCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.ResponseCommand;
import com.vanvalt.payhui.modules.pay.api.util.ErrorCode;
import com.vanvalt.payhui.modules.pay.entity.Download;
import com.vanvalt.payhui.modules.pay.service.DownloadService;
import com.vanvalt.payhui.modules.pay.service.UserService;
import com.vanvalt.payhui.util.constat.Constant;
import com.vanvalt.payhui.util.json.JsonConversion;


/**
 * 软件下载处理
 *
 */
public class DownloadProcess extends BaseProcess {
	/**
	 *日志.
	 */
	private static Logger 				logger = Logger.getLogger(DownloadProcess.class);
	/*private static HttpServletRequest	httpServletRequest	= ((ServletRequestAttributes) RequestContextHolder
			.getRequestAttributes()).getRequest();*/
	
	private UserService userService;
	private DownloadService downloadService;
	
	/**
	 * 构造方法.
	 */	
	public DownloadProcess() {
		super();
	}

	/**
	 * @param downloadService
	 */
	public DownloadProcess(DownloadService downloadService){
		super();
		this.downloadService = downloadService;
	}
	
	/**
	 * @param userService
	 * @param downloadService
	 */
	public DownloadProcess(UserService userService, DownloadService downloadService){
		super();
		this.userService = userService;
		this.downloadService = downloadService;
	}

	/**
	 * 软件下载列表
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public ResponseCommand list(PageCommand pageCommand, RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException {
		
		// 分页参数
		pageCommand = initPagecommand(pageCommand, requestCommand);
		RowBounds rowBounds = new RowBounds(pageCommand.getPage().getOffset(), 
				pageCommand.getPage().getPageSize());
				
		DownloadCommand downloadCommand = new DownloadCommand(requestCommand.getObject().toString());
		Map<String, Object> map = downloadCommand.getMap();
		String uid = (String) map.get("uid");
        String currentPage = (String) map.get("currentPage");
        String pageSize = (String) map.get("pageSize");
		
		List<Map<String,Object>> resultList = Lists.newArrayList();
		
		if(map.isEmpty() && StringUtils.isBlank(uid) && StringUtils.isBlank(currentPage) && StringUtils.isBlank(pageSize)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	User user = userService.get(uid);
        	
        	if(user == null){
        		return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_NOT_EXIST, responseCommand);
        	} else {
        		
        		/*Integer currentPages = Integer.parseInt(currentPage);
                Integer lineSize = Integer.parseInt(pageSize);*/
                
                Download downloadParams = new Download();
                downloadParams.setDelFlag(Constant.DEL_FLAG_NORMAL); // 正常
                downloadParams.setOffset(rowBounds.getOffset());
                downloadParams.setLimit(rowBounds.getLimit());
                
               /* Page<Download> page = downloadService.findPage(new Page<Download>(currentPages, lineSize), downloadParams); 
                List<Download> downloadList = page.getList();*/
                
                List<Download> downloadList = downloadService.findListByPage(downloadParams);
                
                String baseUrl = Global.getConfig("web.file.download.url");
                
                for(Download download:downloadList){
                	
                	Map<String,Object> resultMap = Maps.newHashMap();
                	
                	resultMap.put("title", download.getTitle());
                	String image = download.getImage();
                	String imgUrl = "";
                	if(image != null && !"".equals(image)){
                		imgUrl = baseUrl + image;
                	} 
                	resultMap.put("link", download.getDownloadLink());
                	resultMap.put("image", imgUrl);
                	
                	resultList.add(resultMap);
                }
         		
         		return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
         				JsonConversion.writeListJSON(resultList), responseCommand);
        	}
        	
        }
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public DownloadService getDownloadService() {
		return downloadService;
	}

	public void setDownloadService(DownloadService downloadService) {
		this.downloadService = downloadService;
	}

	
	
}
